package com.yang.jxj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.jxj.entity.Bonus;
import com.yang.jxj.entity.Score;
import com.yang.jxj.service.impl.BonusServiceImpl;
import com.yang.jxj.service.impl.ScoreServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yang
 * @since 2024-06-23
 */
@RestController
@RequestMapping("/api/score")
public class ScoreController {

    @Autowired
    private ScoreServiceImpl sco;
    @Autowired
    private BonusServiceImpl bonu;
    @Autowired
    private com.yang.jxj.Utils.totalScore totalScore;


    @PostMapping("/add")
    public HashMap<String,Object> add(@RequestBody Bonus bonu1) {
        HashMap<String,Object> map = new HashMap<>();
        System.out.println(bonu1);
        QueryWrapper<Bonus> Wrapper = new QueryWrapper<>();
        Wrapper.eq("student_id", bonu1.getStudentId());
        Wrapper.eq("b_project", bonu1.getbProject());
        Wrapper.eq("creat_time", bonu1.getCreatTime());
        Score scoById = sco.getById(bonu1.getStudentId());
        Bonus bonus = bonu.getOne(Wrapper);
        if(bonus.getType().equals(0)){
            scoById.setCreatScore(scoById.getCreatScore()+Integer.parseInt(bonus.getbPoints()));
            boolean b = sco.saveOrUpdate(scoById);
            if(b){
                bonus.setStatus("审核通过");
                bonu.saveOrUpdate(bonus);
                map.put("code",0);
                map.put("msg","审批通过");
                return  map;
            }else{
                map.put("code",1);
                map.put("msg","审批失败");
                return map;
            }
        }else{
            scoById.setFujScore(scoById.getFujScore()+Integer.parseInt(bonus.getbPoints()));
            boolean b = sco.saveOrUpdate(scoById);
            if(b){
                map.put("code",0);
                map.put("msg","审批通过");
                return  map;
            }else{
                map.put("code",1);
                map.put("msg","审批失败");
                return map;
            }
        }

    }


    @GetMapping("/show")   //分页查询
    public IPage<Score> findPage(@RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize,
                                 @RequestParam String studentId
                                 ) {
        Page<Score> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Score> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.like("student_id", studentId);
        objectQueryWrapper.orderByAsc("student_id");
        return sco.page(page, objectQueryWrapper);

    }
    @PostMapping("/save")
    public  HashMap<String,Object> save(@RequestBody Score score){
        HashMap<String, Object> map = new HashMap<>();
        boolean b = sco.saveOrUpdate(score);
        if(b){
            totalScore.TotalScore(score.getStudentId());
            map.put("code",0);
            map.put("msg","成功");
            return map;
        }else{
            map.put("code",1);
            map.put("msg","失败");
            return map;
        }
    }

}


